package com.huike.business.service.impl;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.BusinessService;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author STJ12138
 * @version 1.0
 * @description: TODO
 * @date 2023/7/12 15:03
 */
@Service
public class BusinessServiceImpl extends BaseController implements BusinessService {
    @Resource
    private TbBusinessMapper businessMapper;
    @Resource
    private TbAssignRecordMapper tbAssignRecordMapper;
    @Resource
    private SysUserMapper userMapper;

    /**
     * @param tbBusiness
     * @description: 新增商机
     * @param: tbBusiness
     * @return: java.lang.Long
     * @author STJ12138
     * @date: 2023/7/12 15:08
     */
    @Transactional
    @Override
    public Long add(TbBusiness tbBusiness) {
        //tbBusiness由tb_clue、tb_business、tb_assign_record三个表组成
        //需要将字段添加到三个表中
        // TbClue tbClue = new TbClue();
        // BeanUtils.copyProperties(tbBusiness,tbClue);
        // clueMapper.insert(tbClue);
        // Long clueId = tbClue.getId();
        //
        // tbBusiness.setClueId(clueId);
        if (tbBusiness.getSex() == null){
            tbBusiness.setSex("男");
        }
        tbBusiness.setStatus("1");
        businessMapper.insert(tbBusiness);
        Long id = tbBusiness.getId();

        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setAssignId(id);
        tbAssignRecord.setUserId(SecurityUtils.getUserId());
        tbAssignRecord.setUserName(SecurityUtils.getUsername());
        tbAssignRecord.setDeptId(SecurityUtils.getDeptId());
        tbAssignRecord.setCreateTime(DateUtils.getNowDate());
        tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
        tbAssignRecord.setLatest("1");
        tbAssignRecord.setType("1");
        tbAssignRecordMapper.insert(tbAssignRecord);
        return id;
    }

    /**
     * @param tbBusiness
     * @description: 修改商机
     * @param: tbBusiness
     * @return: void
     * @author STJ12138
     * @date: 2023/7/12 17:13
     */
    @Transactional
    @Override
    public void update(TbBusiness tbBusiness) {
        tbBusiness.setUpdateBy(SecurityUtils.getUsername());
        tbBusiness.setUpdateTime(DateUtils.getNowDate());
        businessMapper.update(tbBusiness);
    }

    /**
     * @param assignmentVo
     * @description: 商机分配
     * @param: assignmentVo
     * @return: void
     * @author STJ12138
     * @date: 2023/7/12 18:41
     */
    @Override
    public void assignment(AssignmentVo assignmentVo) {
        //商机列表的分配
        //根据userId查询到部门和姓名等信息
        SysUser user = userMapper.selectUserById(assignmentVo.getUserId());
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setUserId(user.getUserId());
        tbAssignRecord.setUserName(user.getUserName());
        tbAssignRecord.setDeptId(user.getDeptId());
        tbAssignRecord.setCreateTime(DateUtils.getNowDate());
        tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
        tbAssignRecord.setLatest("1");

        //根据商机表和关联表连表查询出关联表的id数组，根据数组修改关联表的数据
        Long[] ids = assignmentVo.getIds();
        //判断是商机列表还是商机池  由于是批量操作，所以只需要查询一个就行
        TbBusiness byId = businessMapper.getById(ids[0]);
        if ("1".equals(byId.getStatus()) || "2".equals(byId.getStatus())) {
            businessMapper.assignment(tbAssignRecord, ids);
        }

        //商机池的分配
        //因为商机池没有关联表，所以分配即添加关联表，并将状态修改为带跟进（status=1）
        if ("3".equals(byId.getStatus())){
            TbBusiness tbBusiness = new TbBusiness();
            tbBusiness.setStatus("1");
            for (Long id : ids){
                tbAssignRecord.setAssignId(id);
                tbBusiness.setId(id);
                tbAssignRecordMapper.insert(tbAssignRecord);
                businessMapper.update(tbBusiness);
            }
        }
    }

    /** 
     * @description: 查询商机列表 
     * @param: tbBusiness 
     * @return: com.huike.common.core.page.TableDataInfo<java.util.List<com.huike.business.domain.TbBusiness>> 
     * @author STJ12138
     * @date: 2023/7/12 21:57
     */
    @Override
    public TableDataInfo<List<TbBusiness>> list(TbBusiness tbBusiness) {
        startPage();
        Map<String, Object> params = tbBusiness.getParams();
        tbBusiness.setBeginCreateTime((String) params.get("beginCreateTime"));
        tbBusiness.setEndCreateTime((String) params.get("endCreateTime"));
        List<TbBusiness> list = businessMapper.list(tbBusiness);
        return getDataTable(list,params);
    }

    /**
     * @param id
     * @description: 获取商机详细信息
     * @param: id
     * @return: com.huike.business.domain.TbBusiness
     * @author STJ12138
     * @date: 2023/7/12 22:16
     */
    @Override
    public TbBusiness getById(Long id) {
        TbBusiness tbBusiness = businessMapper.getById(id);
        return tbBusiness;
    }

    /**
     * @param ids
     * @description: 删除商机
     * @param: ids
     * @return: void
     * @author STJ12138
     * @date: 2023/7/12 22:32
     */
    @Override
    public void delete(List<Long> ids) {
        businessMapper.delete(ids);
    }

    /**
     * @param id
     * @param reason
     * @description: 踢回公海
     * @param: id
     * reason
     * @return: void
     * @author STJ12138
     * @date: 2023/7/12 23:05
     */
    @Override
    public void back(Long id, String reason) {
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setId(id);
        tbBusiness.setStatus("3");
        tbBusiness.setEndTime(DateUtils.getNowDate());
        //删除该商机关联表
        tbAssignRecordMapper.deleteByBusinessId(id);
        businessMapper.update(tbBusiness);
    }

    /**
     * @param tbBusiness
     * @description: 查询公海池
     * @param: tbBusiness
     * @return: com.huike.common.core.page.TableDataInfo<java.util.List < com.huike.business.domain.TbBusiness>>
     * @author STJ12138
     * @date: 2023/7/12 23:18
     */
    @Override
    public TableDataInfo<List<TbBusiness>> pool(TbBusiness tbBusiness) {
        startPage();
        Map<String, Object> params = tbBusiness.getParams();
        tbBusiness.setBeginCreateTime((String) params.get("beginCreateTime"));
        tbBusiness.setEndCreateTime((String) params.get("endCreateTime"));
        tbBusiness.setStatus("3");
        List<TbBusiness> list = businessMapper.pool(tbBusiness);
        return getDataTable(list,params);
    }


}
